/**
 * 
 */
package com.point.business.tags;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.point.business.database.model.User;
import com.point.business.util.Role;

/**
 * @author Ilija Trajkoski
 * 
 */
public class IsLoggedTag extends TagSupport {
	private static Logger logger = Logger.getLogger(IsLoggedTag.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 5907115007078842505L;
	StringWriter sw = new StringWriter();

	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		HttpServletResponse response = (HttpServletResponse) pageContext
				.getResponse();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			try {
				if (user.getRole() == Role.ADMIN) {
					response.sendRedirect(request.getContextPath() + "/admin/admin.jsp");
				} else {
					response.sendRedirect(request.getContextPath() + "/user/user.jsp");
				}
			} catch (IOException e) {
				logger.error(e);
			}
		}
		return SKIP_BODY;
	}
}
